﻿using System;
using System.Windows.Forms;
using agsXMPP;
using Lapd.Common.CustomStanzas;
using agsXMPP.protocol.client;

namespace Lapd.PoC
{
    public partial class WriteOnSampleTopic : Form
    {
        private XmppClientConnection xmppClientConnection;

        public WriteOnSampleTopic(XmppClientConnection xmppClientConnection)
        {
            InitializeComponent();

            this.xmppClientConnection = xmppClientConnection;
        }

        private void MitWriteClick(object sender, EventArgs e)
        {
            // Prepare SddsIq WRITE packet
            Jid jidTo = new Jid(CustomStanzasConstants.JidToPreffixConst + "." + Session.Server);
            Jid jidFrom = new Jid(Session.Jid);
            
            Write write = new Write(txtTopic.Text);
            SampleField sampleField1 = new SampleField("int", txtMoteId.Text);
            SampleField sampleField2 = new SampleField("int", txtSample.Text);
            SampleField sampleField3 = new SampleField("string", txtVariableName.Text);
            SampleField sampleField4 = new SampleField("string", txtData.Text);
            write.AddSampleField(sampleField1);
            write.AddSampleField(sampleField2);
            write.AddSampleField(sampleField3);
            write.AddSampleField(sampleField4);

            Sdds sdds = new Sdds(write);
            SddsIq sddsIq = new SddsIq(sdds);
            sddsIq.Type = IqType.set;
            sddsIq.To = jidTo;
            sddsIq.From = jidFrom;

            xmppClientConnection.Send(sddsIq);

            this.Close();
        }

        private void MitCancelClick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}